<!DOCTYPE html>
<head>
<meta charset="utf-8">
<style>
</style>
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Roboto+Condensed:wght@400;700&display=swap" rel="stylesheet"> 
<link rel="stylesheet" href="post.css">
<script src="https://corteousquestions.club/libraries/w3.js"></script>
<link href="https://galgoesgalgado.club/libraries/prism/themes/prism-duotone-light.css" rel="stylesheet" /> 
</head>
<body class="language-python">
<div class="grid-container">
	</br>

  </br>
  </br>  </br>
  </br>
 <div class="col_center">
 <div class="highlight">
 <p class="has-line-data" data-line-start="0" data-line-end="26">I am very sure that I must not be the first person that stopped<br>
writing in a blog, because tinkering with it took all the time<br>
he had.<br>
I understand now the emphasys some people give to a clean<br>
environment to write in, free from distractions.<br>
That is not the environment that I’m in right now. Making the blog<br>
as I go along, has pushed me in several directions, all of them<br>
interesting, all of them adding something to the site and, all<br>
orthogonal to each other.<br>
So, to take a breather, I stopped to write this post.<br>
After trying the simplicity of the flat files and static sites, I<br>
noticed wistfully, that their system did without a database. In fact<br>
it was one of its selling points. No added complexity from<br>
managing a batabase, they say. Well, after trying static files I<br>
was reminded that their simplicity came at the cost of ease of use,<br>
as it usually is. Or, more realistically, it’s just a reflection<br>
of my own ignorance. I was having a dificult time understanding the<br>
management and manipulation of the metadata. In this static files<br>
solutions, it’s all done with files that get information through javascript template tricks that I didn’t understand at all.<br>
That was another thing that started tugging at me. Learn a bit of javascript.<br>
But a database I understand. At least I understand those that cater to my<br>
simple needs. So I created a small python class to help collect and keep some metadata on<br>
my posts, that I’ll keep on a mysql database.<br>
This is what I did:</p>
<ol>
<li class="has-line-data" data-line-start="27" data-line-end="31">I imported <a href="https://www.journaldev.com/23788/python-string-module">string</a>, to<br>
capitalize words without having to worry with errors coming from apostrophes,<br>
sultan because it’s easier that subprocess, mysql.connector to link to the db<br>
and colr to make it less drab.</li>
</ol>
<pre><code class="has-line-data" data-line-start="32" data-line-end="37" class="language-python"><span class="hljs-keyword">import</span> string
<span class="hljs-keyword">from</span> sultan.api <span class="hljs-keyword">import</span> Sultan
<span class="hljs-keyword">from</span> mysql.connector <span class="hljs-keyword">import</span> connect
<span class="hljs-keyword">from</span> colr <span class="hljs-keyword">import</span> color
</code></pre>
<ol start="2">
<li class="has-line-data" data-line-start="38" data-line-end="40">Opened the class and defined the global variable ‘tit’ for title, because it<br>
soon became clear that it would be needed for a lot of methods.</li>
</ol>
<pre><code class="has-line-data" data-line-start="41" data-line-end="46" class="language-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Meta</span><span class="hljs-params">()</span>:</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, tit)</span>:</span>
        self.tit = input(color(<span class="hljs-string">'  [T] - Title? '</span>, fore=<span class="hljs-string">'#fa2033'</span>))
</code></pre>
<ol start="3">
<li class="has-line-data" data-line-start="47" data-line-end="49">Title method. Collects the title chosen by the user and capitalizes the<br>
first letter of every word.</li>
</ol>
<pre><code class="has-line-data" data-line-start="50" data-line-end="56" class="language-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">title</span><span class="hljs-params">(self)</span>:</span>
    <span class="hljs-string">""" Collects the title chosen by the user and capitalizes the
    first letter of every word. """</span>
    self.title = string.capwords(self.tit)  <span class="hljs-comment"># https://bit.ly/3pMw9ew</span>
    <span class="hljs-keyword">return</span> self.title
</code></pre>
<ol start="4">
<li class="has-line-data" data-line-start="57" data-line-end="58">Create a db entry with the url of the post.</li>
</ol>
<pre><code class="has-line-data" data-line-start="59" data-line-end="66" class="language-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">link</span><span class="hljs-params">(self)</span>:</span>
    <span class="hljs-string">""" Creates an entry with a link where the post will be. """</span>
    slug = input(color(<span class="hljs-string">'  [S] - Slug? '</span>, fore=<span class="hljs-string">'#fa2033'</span>))
    lnk = <span class="hljs-string">'/usr/share/nginx/html/analytics/ascii/public/pages/posts/'</span>\
          + slug + <span class="hljs-string">".html"</span>
    <span class="hljs-keyword">return</span> lnk
</code></pre>
<ol start="5">
<li class="has-line-data" data-line-start="67" data-line-end="68">Create a draft page.</li>
</ol>
<pre><code class="has-line-data" data-line-start="69" data-line-end="77" class="language-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">page</span><span class="hljs-params">(self)</span>:</span>
    <span class="hljs-string">""" Creates the draft page. """</span>
    s = Sultan()
    draft_path = <span class="hljs-string">"/usr/share/nginx/html/analytics/ascii/drafts/"</span> \
        + <span class="hljs-string">"'"</span> + self.tit + <span class="hljs-string">"'"</span> + <span class="hljs-string">".md"</span>
    <span class="hljs-keyword">with</span> Sultan.load() <span class="hljs-keyword">as</span> s:
        s.touch(draft_path).run()
</code></pre>
<ol start="6">
<li class="has-line-data" data-line-start="78" data-line-end="79">Asks the user for a description of the post.</li>
</ol>
<pre><code class="has-line-data" data-line-start="80" data-line-end="86" class="language-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">description</span><span class="hljs-params">(self)</span>:</span>
    <span class="hljs-string">""" Asks the user for a description of the post. """</span>
    descript = input(color(<span class="hljs-string">'  [D] - Write a description: '</span>,
                           fore=<span class="hljs-string">'#fa2033'</span>))
    <span class="hljs-keyword">return</span> descript
</code></pre>
<ol start="7">
<li class="has-line-data" data-line-start="87" data-line-end="88">Ask for tags.</li>
</ol>
<pre><code class="has-line-data" data-line-start="89" data-line-end="94" class="language-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">tags</span><span class="hljs-params">(self)</span>:</span>
    <span class="hljs-string">""" Asks the user for the tags of the post. """</span>
    tgs = input(color(<span class="hljs-string">'  [T] - Tags? '</span>, fore=<span class="hljs-string">'#fa2033'</span>))
    <span class="hljs-keyword">return</span> tgs
</code></pre>
<ol start="8">
<li class="has-line-data" data-line-start="95" data-line-end="96">Asks for categories.</li>
</ol>
<pre><code class="has-line-data" data-line-start="97" data-line-end="102" class="language-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">categories</span><span class="hljs-params">(self)</span>:</span>
    <span class="hljs-string">""" Asks the user for the categories of the post. """</span>
    cat = input(color(<span class="hljs-string">'  [C] - Choose the Categories '</span>, fore=<span class="hljs-string">'#fa2033'</span>))
    <span class="hljs-keyword">return</span> cat
</code></pre>
<ol start="9">
<li class="has-line-data" data-line-start="103" data-line-end="104">Turns all function results in stringsso it can be sent as a query.</li>
</ol>
<pre><code class="has-line-data" data-line-start="105" data-line-end="114" class="language-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">answers</span><span class="hljs-params">(self)</span>:</span>
    tit = str(self.title())
    desc = str(self.description())
    tags = str(self.tags())
        cat = str(self.categories())
        lnk = str(self.link())
        answrs = [tit, desc, tags, cat, lnk]
        <span class="hljs-keyword">return</span> answrs
    </code></pre>
    <ol start="10">
    <li class="has-line-data" data-line-start="115" data-line-end="116">Send it to the database.</li>
    </ol>
    <pre><code class="has-line-data" data-line-start="117" data-line-end="131" class="language-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">connection</span><span class="hljs-params">(self)</span>:</span>
        self.page()
        conn = connect(
                host=<span class="hljs-string">"localhost"</span>,
                user=<span class="hljs-string">"root"</span>,
                password=<span class="hljs-string">"xxxx"</span>,
                database=<span class="hljs-string">"dazed"</span>)
        cur = conn.cursor()
        query = <span class="hljs-string">""" INSERT INTO dazed (title, description, tags, categories, link)
                VALUES (%s, %s, %s, %s, %s) """</span>
        cur.execute(query, self.answers())
        conn.commit()
        conn.close()
    </code></pre>
    <p class="has-line-data" data-line-start="132" data-line-end="133">Here is the whole code:</p>
    <pre><code class="has-line-data" data-line-start="135" data-line-end="213" class="language-python">
    <span class="hljs-string">""" This class will house the functions for the creation of the\
    post's metadata. """</span>
    <span class="hljs-keyword">import</span> string
    <span class="hljs-keyword">from</span> sultan.api <span class="hljs-keyword">import</span> Sultan
    <span class="hljs-keyword">from</span> mysql.connector <span class="hljs-keyword">import</span> connect
    <span class="hljs-keyword">from</span> colr <span class="hljs-keyword">import</span> color
    
    
    <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Meta</span><span class="hljs-params">()</span>:</span>
    
        <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, tit)</span>:</span>
            self.tit = input(color(<span class="hljs-string">'  [T] - Title? '</span>, fore=<span class="hljs-string">'#fa2033'</span>))
    
        <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">title</span><span class="hljs-params">(self)</span>:</span>
            <span class="hljs-string">""" Collects the title chosen by the user and capitalizes the
            first letter of every word. """</span>
            self.title = string.capwords(self.tit)  <span class="hljs-comment"># https://bit.ly/3pMw9ew</span>
            <span class="hljs-keyword">return</span> self.title
    
        <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">link</span><span class="hljs-params">(self)</span>:</span>
            <span class="hljs-string">""" Creates an entry with a link where the post will be. """</span>
            slug = input(color(<span class="hljs-string">'  [S] - Slug? '</span>, fore=<span class="hljs-string">'#fa2033'</span>))
            lnk = <span class="hljs-string">'/usr/share/nginx/html/analytics/ascii/public/pages/posts/'</span>\
                  + slug + <span class="hljs-string">".html"</span>
            <span class="hljs-keyword">return</span> lnk
    
        <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">page</span><span class="hljs-params">(self)</span>:</span>
            <span class="hljs-string">""" Creates the draft page. """</span>
            s = Sultan()
            draft_path = <span class="hljs-string">"/usr/share/nginx/html/analytics/ascii/drafts/"</span> \
                + <span class="hljs-string">"'"</span> + self.tit + <span class="hljs-string">"'"</span> + <span class="hljs-string">".md"</span>
            <span class="hljs-keyword">with</span> Sultan.load() <span class="hljs-keyword">as</span> s:
                s.touch(draft_path).run()
    
        <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">description</span><span class="hljs-params">(self)</span>:</span>
            <span class="hljs-string">""" Asks the user for a description of the post. """</span>
                    descript = input(color(<span class="hljs-string">'  [D] - Write a description: '</span>,
                                           fore=<span class="hljs-string">'#fa2033'</span>))
                    <span class="hljs-keyword">return</span> descript
            
                <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">tags</span><span class="hljs-params">(self)</span>:</span>
                    <span class="hljs-string">""" Asks the user for the tags of the post. """</span>
                    tgs = input(color(<span class="hljs-string">'  [T] - Tags? '</span>, fore=<span class="hljs-string">'#fa2033'</span>))
                    <span class="hljs-keyword">return</span> tgs
            
                <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">categories</span><span class="hljs-params">(self)</span>:</span>
                    <span class="hljs-string">""" Asks the user for the categories of the post. """</span>
                    cat = input(color(<span class="hljs-string">'  [C] - Choose the Categories '</span>, fore=<span class="hljs-string">'#fa2033'</span>))
                    <span class="hljs-keyword">return</span> cat
            
                <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">answers</span><span class="hljs-params">(self)</span>:</span>
                    tit = str(self.title())
                    desc = str(self.description())
                    tags = str(self.tags())
                    cat = str(self.categories())
                    lnk = str(self.link())
                    answrs = [tit, desc, tags, cat, lnk]
                    <span class="hljs-keyword">return</span> answrs
            
                <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">connection</span><span class="hljs-params">(self)</span>:</span>
                    self.page()
                    conn = connect(
                            host=<span class="hljs-string">"localhost"</span>,
                            user=<span class="hljs-string">"root"</span>,
                            password=<span class="hljs-string">"xxxx"</span>,
                            database=<span class="hljs-string">"dazed"</span>)
                    cur = conn.cursor()
                    query = <span class="hljs-string">""" INSERT INTO dazed (title, description, tags, categories, link)
                            VALUES (%s, %s, %s, %s, %s) """</span>
                    cur.execute(query, self.answers())
                    conn.commit()
                    conn.close()
            
            
            meta = Meta(<span class="hljs-string">'0'</span>)
            meta.connection()
            </code></pre>
</br>
<div w3-include-html="https://galgoesgalgado.club/partials/pagination.html">
</div>
</br>
</br>
</br>
</br>
<div w3-include-html="https://galgoesgalgado.club/partials/footer.html">
</div>
  </div>
 </div>
</div>
<script>w3.includeHTML();</script> 
<script src="https://galgoesgalgado.club/libraries/prism/prism.js"></script>
</div>
</body>
</html>
